home *** CD-ROM | disk | FTP | other *** search
/ PC User 2003 December / Australian PC User - December 2003 (CD2).iso / software / apps / files / dwmx2k4.exe / Disk1 / data1.cab / Configuration_En / Commands / Format Table.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  18.1 KB  |  495 lines

  1. // Copyright 1998, 1999, 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved.
  2.  
  3. // *************** GLOBAL VARS  *****************
  4.  
  5. var helpDoc = MM.HELP_cmdFormatTable;
  6. var DEFAULT_tableStyle = 7;
  7. //these constants used in tableFormats.js file when defining new table formats
  8. var NONE=0,LEFT=1,CENTER=2,RIGHT=3,BOLD=1,ITALIC=2,BOLD_ITALIC=3;
  9.  
  10. //******************* API **********************
  11.  
  12. function commandButtons(){
  13.   var tableObj = findTable();
  14.   var Buttons=new Array(MM.BTN_OK,        "applyFormatToSelectedTable();window.close();",
  15.                         MM.BTN_Apply,     "applyFormatToSelectedTable()",
  16.                         MM.BTN_Cancel,    "window.close()",
  17.                                                 MM.BTN_Help,      "displayHelp()");
  18.   if (isUnrecognizedTable(tableObj) || hasCaption(tableObj))
  19.     Buttons=new Array(MM.BTN_OK,"window.close()",
  20.                       MM.BTN_Cancel,"window.close()");
  21.  
  22.   return Buttons;
  23. }
  24.  
  25. function canAcceptCommand(){
  26.   if (dw.getDocumentDOM() == null)
  27.     return false;
  28.   else if (dw.getFocus() != 'document')
  29.     return false;
  30.   else if (dw.getDocumentDOM().getParseMode() != 'html')
  31.     return false;
  32.   else if (dw.appName.indexOf("Ringo") >= 0)
  33.   {
  34.     if (dw.getDocumentDOM().isTableLocked())
  35.       return false;
  36.     else if (dw.getDocumentDOM().getShowLayoutView())
  37.       return false;
  38.     else if (dw.getDocumentDOM().getCCSharedSetting_TextOnlyInNonTemplates())
  39.       return false;
  40.     else if (findTable())
  41.       return true;
  42.     else
  43.       return false;
  44.   }
  45.   else if (findTable())
  46.      return true;
  47.   else
  48.     return false;
  49. }
  50.  
  51.  
  52. //***************** LOCAL FUNCTIONS  ******************
  53.  
  54. function findTable(){
  55.   var tableObj="";
  56.   var selObj = dw.getDocumentDOM().getSelectedNode();
  57.  
  58.   while (tableObj=="" && selObj.parentNode){
  59.     if (selObj.nodeType == Node.ELEMENT_NODE && selObj.tagName=="TABLE")
  60.       tableObj=selObj;
  61.     else
  62.       selObj = selObj.parentNode;
  63.   }
  64.   return tableObj;
  65. }
  66.  
  67. function updatePreview(){
  68.   alternateRows(dwscripts.findDOMObject("presetNames").selectedIndex,getPreviewTable());
  69. }
  70.  
  71. function removeTag( theObj, Tag )
  72. {
  73.     var children   = theObj.childNodes;
  74.     var nChildren  = children.length;
  75.  
  76.     for( var i = 0; i < nChildren; i++ )
  77.     {
  78.          var currentChild = children.item(i);
  79.  
  80.          if ( currentChild.hasChildNodes() )
  81.             removeTag( currentChild, Tag );
  82.  
  83.          if ( currentChild.nodeType == Node.ELEMENT_NODE &&
  84.               currentChild.tagName == Tag )
  85.               currentChild.outerHTML = currentChild.innerHTML;
  86.      }
  87. }
  88.  
  89.  
  90. function applyFormatToSelectedTable(){
  91.  
  92.   var selObj,selArr;
  93.     var dom = dw.getDocumentDOM()
  94.   
  95.   // get current selection
  96.   selArr = dom.getSelection();
  97.   selObj = dom.offsetsToNode(selArr[0],selArr[1]);
  98.       
  99.   alternateRows(dwscripts.findDOMObject("presetNames").selectedIndex,findTable());
  100.   
  101.   // restore original selection, if it still exists; if not, just select the table.
  102.   if (dw.nodeExists(selObj)){
  103.     selArr = dom.nodeToOffsets(selObj);
  104.   }else{
  105.     selArr = dom.nodeToOffsets(findTable());
  106.   }
  107.   dom.setSelection(selArr[0],selArr[1]);
  108.   
  109.   savePreferences();
  110. }
  111.  
  112. function isUnrecognizedTable(tableObj) { //checks selected table
  113.   var counter=0;
  114.   var trIter = tableObj.childNodes;
  115.   var trNode=trIter.item(counter);
  116.   var retVal=false;
  117.   while (trNode && !retVal) {
  118.     if (trNode.tagName!="TR" || trNode.childNodes.length==0) 
  119.       retVal=true;
  120.     else trNode=trIter.item(++counter)
  121.   }
  122.   return retVal;
  123. }
  124.  
  125. function hasCaption(theObj){
  126.   if (theObj.childNodes.item(0).tagName && theObj.childNodes.item(0).tagName=="CAPTION")
  127.     return true;
  128.   return false;
  129. }
  130.  
  131. function initializeUI() { //fill presetNames list
  132.   var mainLayer=dwscripts.findDOMObject("mainLayer");
  133.   var presetNamesList,nSize,Names,i;
  134.   var tableObj=findTable();
  135.   var selectedFormat;
  136.  
  137.  if (hasCaption(tableObj))
  138.      mainLayer.innerHTML="<p> </p>" + MSG_CaptionIsPresent;
  139.  else if (isUltraDev() && hasServerBehaviorApplied(tableObj)) {
  140.      mainLayer.innerHTML = "<p> </p>" + MM.MSG_RegionServerBehaviorsNotAllowed;
  141.  } else if (isUnrecognizedTable(tableObj))
  142.     mainLayer.innerHTML="<p> </p>" + MSG_IsInvalidTable;
  143.  else {
  144.       //add select list options
  145.       mainLayer.visibility="hidden";
  146.       for (i=0;i<4;i++){  //populate select lists
  147.         dwscripts.findDOMObject("topRowAlign").options[i] = new Option(OPTIONS_Align[i]);
  148.         dwscripts.findDOMObject("topRowTextStyle").options[i]=new Option(OPTIONS_Text_Style[i]);
  149.         dwscripts.findDOMObject("leftColAlign").options[i] = new Option(OPTIONS_Align[i]);
  150.         dwscripts.findDOMObject("leftColTextStyle").options[i] = new Option(OPTIONS_Text_Style[i]);
  151.         dwscripts.findDOMObject("rowLimit").options[i] = new Option(OPTIONS_Row_Limit[i]);
  152.       }
  153.       dwscripts.findDOMObject("rowLimit").options[4] = new Option(OPTIONS_Row_Limit[4]);
  154.  
  155.     presetNamesList = dwscripts.findDOMObject("presetNames");
  156.     Names=tableFormats();  //get list of table format names from tableFormats.js file
  157.     nSize = Names.length;
  158.     for (i=0;i<nSize;i++) { 
  159.       presetNamesList.options[i] = new Option(Names[i].name); //populate form field
  160.     }
  161.     dwscripts.findDOMObject("mainLayer").visibility="visible";
  162.     
  163.     selectedFormat = getFormatPreference();
  164.     setUI(selectedFormat); //choose preferred format
  165.     alternateRows(selectedFormat,getPreviewTable()); //apply this format to preview table
  166.     presetNamesList.selectedIndex=selectedFormat; //select default format
  167.  } 
  168. }
  169.  
  170.  
  171. function alternateRowColors(trIter,firstRowColor,secondRowColor,rowLimit,useTD,topRowColor){
  172.   var rowLen=trIter.length;
  173.   var cellLen,tdNode,trNode,counter;
  174.   if (rowLimit==0) rowLimit=1000; //repeat first color if rowLimit is equal to 0 
  175.   var counter=2*rowLimit;
  176.   var startRow = (topRowColor)?1:0;
  177.   for (i=startRow;i<rowLen;i++){
  178.     trNode=trIter.item(i);
  179.     rowColor=(counter%(2*rowLimit)<rowLimit)?firstRowColor:secondRowColor;
  180.     if (!useTD){
  181.       // attach bgcolor attribute to tr tags
  182.       trNode.setAttribute("bgcolor",rowColor);
  183.       // remove bgcolor from td tags, so they don't override
  184.       tdIter=trNode.childNodes; cellLen=tdIter.length;
  185.       for (j=0;j<cellLen;j++){
  186.         if (tdIter.item(j).getAttribute("bgcolor") != null && tdIter.item(j).getAttribute("bgcolor") != undefined){   
  187.           tdIter.item(j).removeAttribute("bgcolor"); 
  188.         }
  189.       }
  190.     } 
  191.     else {
  192.       // attach bgcolor attribute to td tags
  193.       // remove tr settings, if any (they're useless now)
  194.       if (trNode.getAttribute("bgcolor") != null && trNode.getAttribute("bgcolor") != undefined){
  195.         trNode.removeAttribute("bgcolor");
  196.       }
  197.       tdIter=trNode.childNodes; cellLen=tdIter.length;
  198.       for (j=0;j<cellLen;j++) 
  199.         tdIter.item(j).setAttribute("bgcolor",rowColor);
  200.     }
  201.     counter++; 
  202.   }
  203. }
  204.  
  205. function formatTopRow(trIter,topRowColor,topRowTextColor,topRowTextStyle,topRowAlign,useTD){
  206.   var trNode=trIter.item(0), cellLen;
  207.  
  208.   //add bgcolor & align
  209.   if (!useTD) { 
  210.     // attach bgcolor & align to first row TR tag. make sure to
  211.     // remove settings from TDs to prevent them overriding the TR settings.
  212.     if (topRowColor){
  213.         trNode.setAttribute("bgcolor",topRowColor);
  214.       tdIter=trNode.childNodes; cellLen=tdIter.length;
  215.       for (i=0;i<cellLen;i++){
  216.         tdNode=tdIter.item(i);
  217.         if (tdNode.getAttribute("bgcolor") != null && tdNode.getAttribute("bgcolor") != undefined){
  218.           tdNode.removeAttribute("bgcolor");
  219.         }
  220.       }
  221.     }
  222.       if ( !topRowAlign || topRowAlign.toLowerCase() == "none" ){
  223.         trNode.removeAttribute("align");
  224.     }else{
  225.       trNode.setAttribute("align",topRowAlign);
  226.       tdIter=trNode.childNodes; cellLen=tdIter.length;
  227.       for (i=0;i<cellLen;i++){
  228.         tdNode=tdIter.item(i);
  229.         if (tdNode.getAttribute("align") != null && tdNode.getAttribute("align") != undefined){
  230.           tdNode.removeAttribute("align");
  231.         }
  232.       }
  233.     }
  234.   } else { 
  235.     // attach bgcolor & align to TD tags in first row
  236.     // remove settings from TR tag (they're useless now)
  237.     tdIter=trNode.childNodes; cellLen=tdIter.length;
  238.     for (i=0;i<cellLen;i++){
  239.       tdNode=tdIter.item(i);
  240.         if (topRowColor)
  241.         tdNode.setAttribute("bgcolor",topRowColor);
  242.       if ( !topRowAlign || topRowAlign.toLowerCase()=="none" )
  243.           tdNode.removeAttribute("align");
  244.       else
  245.           tdNode.setAttribute("align",topRowAlign);      
  246.     }
  247.     if (trNode.getAttribute("bgcolor") != null && trNode.getAttribute("bgcolor") != undefined){
  248.       trNode.removeAttribute("bgcolor");
  249.     }
  250.     if (trNode.getAttribute("align") != null && trNode.getAttribute("align") != undefined){
  251.       trNode.removeAttribute("align");
  252.     }
  253.   } 
  254.  
  255.   //add text color and text formatting
  256.   tdIter=trNode.childNodes; cellLen=tdIter.length;
  257.    for (i=0;i<cellLen;i++) {
  258.      tdNode=tdIter.item(i);
  259.      //set font color, if specified
  260.      if (topRowTextColor){
  261.        if (!findTag(tdNode,"FONT")){ //if the color attribute cannot be added to a "safe" font tag, add it.
  262.          tdNode.innerHTML='<font color="' + topRowTextColor + '">' + tdNode.innerHTML + '</font>';
  263.        }
  264.        else  { //font tag already exists, set color attribute on it
  265.          findTag(tdNode,"FONT").setAttribute("color",topRowTextColor);
  266.        }
  267.          //remove font color if NOT specified
  268.      }else{
  269.         var fontTag = findTag(tdNode,"FONT");
  270.                  if (fontTag && fontTag.getAttribute("color")){
  271.                     fontTag.removeAttribute("color");
  272.           if (fontTag.attributes.length == 0){
  273.             removeTag(tdNode,"FONT");
  274.           }
  275.                 }
  276.             }
  277.       //set text style
  278.      if (topRowTextStyle==0){//if text style set to none
  279.        removeTag(tdNode,"B"); //remove bold tags
  280.        removeTag(tdNode,"I"); //and remove italic tags
  281.      } else {
  282.        if (topRowTextStyle==1 || topRowTextStyle==3){ //if bold or bold italic
  283.          if (topRowTextStyle==1) //if bold
  284.            removeTag(tdNode,"I"); //remove all italic tags
  285.          if (!findTag(tdNode,"B")){ //if a B tag is not found around the table cell text
  286.            removeTag(tdNode,"B"); //remove any other B tags
  287.            tdNode.innerHTML="<b>" + tdNode.innerHTML + "</b>"; //make first child a B tag
  288.          }
  289.        }
  290.        if (topRowTextStyle==2 || topRowTextStyle==3){ //if italic or or bold italic
  291.          if (topRowTextStyle==2) //if italic
  292.            removeTag(tdNode,"B"); //remove all bold tags
  293.          if (!findTag(tdNode,"I")){ //if an I tag is not found around the table cell text 
  294.            removeTag(tdNode,"I"); //remove any other I tags
  295.            tdNode.innerHTML="<i>" + tdNode.innerHTML + "</i>"; //make first child an I tag
  296.          }
  297.        }
  298.      }
  299.    } 
  300. }
  301.  
  302. function formatLeftCol(trIter,leftColTextStyle,leftColAlign){
  303.   rowLen=trIter.length;
  304.    for (i=0;i<rowLen;i++){
  305.      trNode=trIter.item(i);
  306.      tdNode=trNode.childNodes.item(0);
  307.      //set text style
  308.      if (leftColTextStyle==0){ //if text style set to none
  309.        removeTag(tdNode,"B"); //remove bold tags
  310.        removeTag(tdNode,"I"); //and remove italic tags
  311.      }
  312.      else {
  313.        if (leftColTextStyle==1 || leftColTextStyle==3){ //if text style set to bold or bold italic
  314.          if (leftColTextStyle==1) //if bold
  315.            removeTag(tdNode,"I"); //remove all italic tags
  316.          if (!findTag(tdNode,"B")){ //if B tag is not found around text
  317.            removeTag(tdNode,"B"); //remove all other B tags
  318.            tdNode.innerHTML="<b>" + tdNode.innerHTML + "</b>"; //make first child a B tag
  319.          }
  320.     
  321.        }
  322.        if (leftColTextStyle==2 || leftColTextStyle==3){ //if text style is set to italic or bold italic
  323.          if (leftColTextStyle==2) //if italic
  324.            removeTag(tdNode,"B"); //remove all bold tags
  325.          if (!findTag(tdNode,"I")){ //if i tag is not found around text
  326.            removeTag(tdNode,"I"); //remove all other i tags
  327.            tdNode.innerHTML="<i>" + tdNode.innerHTML + "</i>"; //make first child tag i tag
  328.          }
  329.        }
  330.      }     
  331.      //set alignment
  332.      if ( !leftColAlign || leftColAlign.toLowerCase()=="none" )
  333.        tdNode.removeAttribute("align");
  334.      else
  335.        tdNode.setAttribute("align",leftColAlign);        
  336.    }
  337. }
  338.  
  339. function setUI(presetIndex){
  340.       var Names = tableFormats();
  341.       var thisFormat = Names[presetIndex];               
  342.       with (thisFormat){
  343.         //error check rowLimit value
  344.         if (rowLimit>4 || rowLimit<0)rowLimit=1;
  345.     
  346.         //align attributes are specified as "left","center","right", and "";
  347.         //following function gets correct selected index for alignment option
  348.         topRowAlign=getIndex(topRowAlign);
  349.         leftColAlign=getIndex(leftColAlign); 
  350.         //select the appropriate options
  351.         dwscripts.findDOMObject("rowLimit").selectedIndex=parseInt(rowLimit);
  352.         dwscripts.findDOMObject("topRowAlign").selectedIndex=topRowAlign;
  353.         dwscripts.findDOMObject("topRowTextStyle").selectedIndex=topRowTextStyle;
  354.         dwscripts.findDOMObject("leftColAlign").selectedIndex=leftColAlign;
  355.         dwscripts.findDOMObject("leftColTextStyle").selectedIndex=leftColTextStyle;
  356.       
  357.         //fill in textfields
  358.         dwscripts.findDOMObject("firstRowColor").value = firstRowColor;
  359.         dwscripts.findDOMObject("firstRow").value = firstRowColor;
  360.         dwscripts.findDOMObject("secondRowColor").value = secondRowColor;
  361.         dwscripts.findDOMObject("secondRow").value = secondRowColor;
  362.         dwscripts.findDOMObject("topRow").value = topRowColor;
  363.         dwscripts.findDOMObject("topRowColor").value = topRowColor;
  364.         dwscripts.findDOMObject("topRowTextColor").value = topRowTextColor;
  365.         dwscripts.findDOMObject("topRowText").value = topRowTextColor;
  366.         dwscripts.findDOMObject("borderSize").value = border;
  367.       }
  368.  
  369. }
  370. function alternateRows(presetChoiceIndex,tableObj){
  371.   var tableNode,trIter,trNode,tdIter,tdNode,counter;
  372.   var useTD,topRowColor,selInd,topRowAlign,topRowTextColor;
  373.   var topRowTextStyle,firstRowColor,secondRowColor;
  374.   var rowLimit,borderSize;
  375.  
  376.   tableNode=tableObj;
  377.   trIter=tableNode.childNodes;
  378.   useTD=dwscripts.findDOMObject('useTD').checked?true:false;
  379.   
  380.   //The rest of the function assigns values to below variables based on user interface.
  381.   //User interface is initially populated with argument values, but the user can change
  382.   //them to dynamically update the preview table.
  383.  
  384.   //set variables for the top row
  385.   topRowColor=dwscripts.findDOMObject('topRowColor').value;
  386.   selInd = dwscripts.findDOMObject('topRowAlign').selectedIndex;
  387.   topRowAlign=dwscripts.findDOMObject('topRowAlign').options[selInd].value;
  388.   topRowTextColor=dwscripts.findDOMObject('topRowTextColor').value;
  389.   topRowTextStyle=dwscripts.findDOMObject('topRowTextStyle').selectedIndex;
  390.   
  391.   //set variables for the left col
  392.   selInd = dwscripts.findDOMObject('leftColAlign').selectedIndex;
  393.   leftColAlign=dwscripts.findDOMObject('leftColAlign').options[selInd].value;
  394.   leftColTextStyle=dwscripts.findDOMObject('leftColTextStyle').selectedIndex;
  395.   
  396.   //set variables for the row Colors
  397.   firstRowColor=dwscripts.findDOMObject('firstRowColor').value;
  398.   secondRowColor=dwscripts.findDOMObject('secondRowColor').value;
  399.   rowLimit=dwscripts.findDOMObject("rowLimit").selectedIndex;
  400.   
  401.   //set border size
  402.   borderSize=dwscripts.findDOMObject("borderSize").value;
  403.  
  404.   //Now, use these values to format the table...
  405.   
  406.   //set table border
  407.   tableNode.setAttribute("border",borderSize); 
  408.  
  409.   //alternate row Colors  
  410.   alternateRowColors(trIter,firstRowColor,secondRowColor,rowLimit,useTD,topRowColor);
  411.  
  412.   //add left col formatting:text style & alignment
  413.   formatLeftCol(trIter,leftColTextStyle,leftColAlign);
  414.  
  415.   //add top row formatting:text style,alignent,row color, & text color
  416.   formatTopRow(trIter,topRowColor,topRowTextColor,topRowTextStyle,topRowAlign,useTD);
  417.   
  418.   //set border
  419.   tableNode.setAttribute("border",borderSize);
  420.  
  421.   //if there is a background color, remove it.
  422.   if (tableNode.getAttribute("bgColor")) {
  423.     tableNode.removeAttribute("bgColor");
  424.   }
  425. }
  426.  
  427. function updatePreviewTable(){
  428.   var presetIndex = dwscripts.findDOMObject("presetNames").selectedIndex;
  429.   var previewTable = getPreviewTable();
  430.   var mainLayer = dwscripts.findDOMObject("mainLayer");
  431.   
  432.   setUI(presetIndex);
  433.   alternateRows(dwscripts.findDOMObject("presetNames").selectedIndex,previewTable);//format table
  434. }
  435.  
  436. function getPreviewTable(){
  437.    //returns preview Table object
  438.    return document.getElementsByTagName("TABLE").item(1);
  439. }
  440.  
  441.  
  442. function getIndex(align){
  443.   //returns align index of UI that matches text of align argument i.e: "center"
  444.   //Note: I didn't use constants here because it is more intuitive to define
  445.   //the alignment attribute following the html syntax of align="attribute"
  446.     switch (align){
  447.      case "left": align=1; break;
  448.      case "center": align=2; break;
  449.      case "right": align=3; break;
  450.      default: align=0; break;
  451.    }
  452.  return align;
  453. }
  454.  
  455. //searches the child nodes of tableCellObj -
  456. //returns the innermost object of tagName that surrounds all of the text in
  457. //the cell. returns empty string if not found.
  458. function findTag(obj, tag) {
  459.   var retVal="";
  460.   while (obj.childNodes && obj.childNodes.length == 1) {
  461.     obj = obj.childNodes.item(0);
  462.    if (obj.nodeType == Node.ELEMENT_NODE && obj.tagName == tag)
  463.      retVal=obj;
  464.   }
  465.   return retVal;
  466.  
  467. }
  468.  
  469.  
  470. function getFormatPreference() {
  471.   var metaFile, savedVal, curVal = DEFAULT_tableStyle;
  472.   if (typeof MMNotes != 'undefined') { // Check for MMNotes extension.
  473.    metaFile = MMNotes.open(document.URL, false);
  474.    if (metaFile) {
  475.      // Form specific settings.
  476.      savedVal = MMNotes.get(metaFile, 'MM_pref_FormatTable');
  477.      MMNotes.close(metaFile);
  478.      if (curVal == parseInt(curVal).toString()) {
  479.        curVal = savedVal;
  480.      }
  481.    }
  482.   }
  483.   return parseInt(curVal);
  484. }
  485.  
  486. function savePreferences() {
  487.   if (typeof MMNotes == 'undefined') {return;} // Check for MMNotes extension.
  488.   var metaFile, curVal;
  489.   metaFile = MMNotes.open(document.URL, true);
  490.   if (metaFile) {
  491.     curVal = MMNotes.set(metaFile, 'MM_pref_FormatTable', findObject("presetNames").selectedIndex);
  492.     MMNotes.close(metaFile);
  493.   }
  494. }
  495.